<template>
  <m-form
      ref="form"
      :item="item" :view="view" :db="db"></m-form>
</template>

<script setup lang="ts">
import type {MetaView} from "../../../types/tool/meta.types.ts";
import type {DaoTypes, DataItem} from "../../../types/tool/dao.types.ts";
import {onBeforeMount, ref} from "vue";
import _ from "lodash";

const props = defineProps<{
  selection: any[],
  view: MetaView,
  db: DaoTypes,
  bean: DataItem
}>()

const form = ref()
const item = ref<any>({})

onBeforeMount(() => {
  item.value = _.cloneDeep(props.bean)
})

function getData() {
  const data: any = form.value.getData()
  const length = props.selection.length
  const id = props.bean.id
  if (id) {
    data.id = id
    return [data]
  } else {
    return _.isEmpty(data) ? [] :
        props.selection.map(row => {
          const _data = _.cloneDeep(data)
          _data.id = row.id
          return _data
        })
  }

  // if (!item.value.saved) data.saved = true
  // return props.values.map(({id}) => ({id, ...data}))
}

defineExpose({getData})

</script>

